package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessPersonnelService;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.PersonnelDemandVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/9/9 15:30
 * 修改备注：
 * ================================================
 */
public class TalentsDetailsActivity extends BaseActivity {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_head)
    SimpleDraweeView ivHead;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.tv_price)
    TextView tvPrice;
    @Bind(R.id.tv_people_num)
    TextView tvPeopleNum;
    @Bind(R.id.tv_demand_name)
    TextView tvDemandName;
    @Bind(R.id.tv_start_date)
    TextView tvStartDate;
    @Bind(R.id.tv_tel)
    TextView tvTel;
    @Bind(R.id.tv_address)
    TextView tvAddress;
    @Bind(R.id.layout_chat_tel)
    LinearLayout layoutChatTel;
    @Bind(R.id.sdv_tel)
    SimpleDraweeView sdvTel;
    @Bind(R.id.layout_tel)
    LinearLayout layoutTel;
    @Bind(R.id.layout_work_time)
    RelativeLayout layoutWorkTime;
    @Bind(R.id.view_work_time)
    View viewWorkTime;
    private PersonnelDemandVO personnelDemandVO;
    private String postType = "";

    public static final String OPERATIONVIEW = "0";
    public static final String OPERATIONCHAT = "1";
    public static final String OPERATIONTEL = "2";

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_talents_details);
        setTitle("人才服务详情");
    }

    @Override
    public void initMember() {
        if (getIntent().hasExtra("PersonnelDemandVO")) {
            Map<String, Object> map = (Map<String, Object>) getIntent().getSerializableExtra("PersonnelDemandVO");
            personnelDemandVO = (PersonnelDemandVO) map.get("vo");
            postType = map.get("postType") + "";
            setView(personnelDemandVO);
        } else if (getIntent().hasExtra("map")) {
            Map<String, String> map = (Map<String, String>) getIntent().getSerializableExtra("map");
            // 通过ID获取实体
            postType = map.get("type");
            BusinessPersonnelService.getTalentsInfo(this, map.get("bannerId"), mHandler);
        }
    }

    private void setView(PersonnelDemandVO personnelDemandVO) {
        if (personnelDemandVO != null && personnelDemandVO.isMyTalents())
            layoutChatTel.setVisibility(View.GONE);

        FrescoUtil.showImageSmall("", ivHead);
        tvTitle.setText(personnelDemandVO.getContent() + "");
        tvName.setText(personnelDemandVO.getContacts() + "");
        tvDemandName.setText(personnelDemandVO.getDemand() != null ? personnelDemandVO.getDemand(): "");
        tvTime.setText(personnelDemandVO.getUpdateTime() + "");

        if (personnelDemandVO.getStartDate() != null && personnelDemandVO.getEndDate() != null) {
            layoutWorkTime.setVisibility(View.VISIBLE);
            viewWorkTime.setVisibility(View.VISIBLE);
            tvStartDate.setText(personnelDemandVO.getStartDate() + "至" + personnelDemandVO.getEndDate());
        } else {
            layoutWorkTime.setVisibility(View.GONE);
            viewWorkTime.setVisibility(View.GONE);
        }
        tvTel.setText(personnelDemandVO.getTelephone() + "");
        tvPrice.setText(personnelDemandVO.getPrice() + "元/人");
        tvAddress.setText(personnelDemandVO.getAddress() + "");
        tvPeopleNum.setText(personnelDemandVO.getNumber()+"");

        BusinessPersonnelService.saveFarmGangOperation(this, personnelDemandVO.getId(), mHandler);

    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_GET_TALENTS_INFO_SUCCESS:
                        callBack(msg.obj);
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        personnelDemandVO = FastJsonUtil.getBeanData(object.toString(), PersonnelDemandVO.class);
        if (personnelDemandVO != null)
            setView(personnelDemandVO);
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @OnClick({R.id.layout_tel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_tel:
                if (personnelDemandVO != null && !StringUtil.isStrEmpty(personnelDemandVO.getTelephone())) {
                    Utils.callPhone(TalentsDetailsActivity.this, personnelDemandVO.getTelephone());
                    if (!StringUtil.isStrEmpty(postType))
                        BusinessPersonnelService.saveTelOperation(this, personnelDemandVO.getId(), new Handler());
                }

                break;
        }
    }
}
